package cn.ilikes.tools.htmls.plugins;

import java.io.File;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.ilikes.tools.htmls.AbPaser;
import cn.ilikes.tools.htmls.PaserException;
import cn.ilikes.tools.htmls.PaserListener;

public class JSoupHtmlPaser extends AbPaser {

	@Override
	protected void paserContent(String url, PaserListener paserListener) throws PaserException {
		try {
			Document doc = Jsoup.connect(url).get();
			//			keywords
			String title = doc.title();
			String keywords = null;
			String description = null;
			Elements xxx = doc.select("meta[name=keywords],[name=description]");
			for (int i = 0; i < xxx.size(); i++) {
				Element itmp = xxx.get(i);
				if (itmp.attr("name").equals("keywords")) {
					keywords = itmp.attr("content");
				} else {
					description = itmp.attr("content");
				}
			}
			String body = doc.body().text();
			paserListener.result(doc.html(), title, keywords, description, body);
		} catch (IOException e) {
			throw new PaserException(e);
		}
		
	}

	public static void main(String[] args) throws Exception {
		File url = new File("/home/ilikes/test/12.html");

		Document doc = Jsoup.connect("http://www.infoq.com/cn/articles/lp-hbase-data-disaster-recovery").get();
		//		keywords
		String title = doc.title();

		String keywords = "";
		Elements xxx = doc.select("meta[name=keywords],[name=description]");
		System.out.println("title : " + title + "  " + xxx.size());
		for (int i = 0; i < xxx.size(); i++) {
			Element itmp = xxx.get(i);
			keywords = keywords + " " + itmp.attr("content");
		}

		System.out.println("title : " + title + "  " + keywords);

	}

}
